﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_KeyWord_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As Keyword = (From a In data.Keywords Where a.KeywordID = hidID.Value).SingleOrDefault
            If Not p Is Nothing Then
                txtKeyWord.Text = IIf(IsNothing(p.Keyword1) = True, "", p.Keyword1)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            'Kiem tra tu khoa da ton tai chua
            Dim q = (From k In data.Keywords Where k.Keyword1.ToUpper = txtKeyWord.Text.Trim.ToUpper And k.KeywordID <> hidID.Value Select k).FirstOrDefault
            If q Is Nothing Then
                Dim p As Keyword = (From o In data.Keywords Where o.KeywordID = hidID.Value).SingleOrDefault
                Dim sTitle As String = q.Keyword1
                Dim sQueryString As String = q.QueryString
                Try
                    p.Keyword1 = txtKeyWord.Text
                    p.QueryString = makeURLFriendly(txtKeyWord.Text.Trim)
                    data.SaveChanges()
                    RemoveKeyWordJavascript(sTitle)
                    RemoveKeyWordJavascript(sQueryString)
                    AddKeyWordJavascript(txtKeyWord.Text)
                    AddKeyWordJavascript(makeURLFriendly(txtKeyWord.Text.Trim))
                    Insert_App_Log("Edit KeyWord:" & txtKeyWord.Text.Trim & "", Function_Name.KeyWord, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật keyword thành công.');window.location ='../../Page/KeyWord/Detail.aspx?id=" & hidID.Value & "';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật keyword thất bại.');", Me.Page, True)
                End Try
            Else
                showError.Visible = True
            End If
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("KeyWord.aspx")
    End Sub
#End Region

End Class
